/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.gatech.gellinservlet.model;

import java.util.ArrayList;
import java.util.Random;
import java.io.Serializable;

/**
 *
 * @author Tyler Powell
 */
public class Team implements IDable , Serializable
{
    private static int totalTeams = 0;
    
    private ArrayList<Student> studentList;
    private String name;
    private int ID;
    private int maxSize;
    private int teamDesignation;
    
    //TODO decide if course and lab id are necessary in this class
    
    public Team(String name, int maxSize, int teamDesignation)
    {
        this.name = name;
        this.maxSize = maxSize;
        studentList = new ArrayList<Student>();
        totalTeams++;
        ID = totalTeams;
        this.teamDesignation = teamDesignation;
    }
    
    public Team()
    {
        this("default", 4, 0);
        studentList = new ArrayList<Student>();
    }
    
    /**
     * Add a student to the this team
     * @param stud The student to be added.
     */
    public void add(Student stud)
    {
        if(studentList.size() < maxSize)
        {
            studentList.add(stud);
        }
        else
        {
            //TODO throw an exception
        }
    }
    
    /**
     * Place Students into teams randomly up to the max team size
     * @param studentList The student list containing students not in teams
     * @param teamMaxSize The most students a team is allowed to have.
     */
    public void populate(ArrayList<Student> studentList)
    {
        Random gen = new Random();
        int teamSize = studentList.size();
        for(int i = 0; !studentList.isEmpty() && (i < maxSize && i < teamSize); i++)
        {
            Student stud = studentList.remove(gen.nextInt(studentList.size()));
            this.add(stud);
        }
        
    }
    
    /**
     * remove the student from the team
     * @param stud 
     */
    public void remove(Student stud)
    {
        studentList.remove(stud);
    }

    @Override
    public int getID()
    {
        return ID;
    }
    
    public String getName() 
    {
        return name;
    }
    
    public int getTeamDesignation()
    {
        return teamDesignation;
    }

    public ArrayList<Student> getStudentList()
    {
        return studentList;
    }

    public static int getTotalTeams()
    {
        return totalTeams;
    }

    public static void setTotalTeams(int totalTeams)
    {
        Team.totalTeams = totalTeams;
    }
    
    public void setName(String name) 
    {
        this.name = name;
    }
    
    public int compareTo(Object o)
    {
        return toString().compareTo(((Team)o).toString());
    }
    
    public String toString()
    {
        return name;
    }
}
